<template>
  <PageWrapper contentFullHeight fixedHeight dense>
    <AdVxeTable @register="registerTable">
      <template #actionCol="{ row }">
        <TableAction
          outside
          :actions="[
            {
              icon: 'clarity:details-line',
              tooltip: t('common.detailText'),
              onClick: handleReadDetail.bind(null, row),
            },
            {
              icon: 'fluent:database-link-24-regular',
              tooltip: t('routes.system.audit.auditEntity.title'),
              auth: 'Root.Admin.Audit.AuditEntity.Read', // 权限控制
              onClick: handleReadAuditEntries.bind(null, row),
            },
          ]"
        />
      </template>
    </AdVxeTable>

    <AuditEntityModal @register="registerAuditEntityModal" />
    <!-- <AuditEntityDrawer @register="registerAuditEntityDrawer" /> -->
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { useI18n } from '@/hooks/web/useI18n';
  import { PageWrapper } from '@/components/Page';
  import { AdVxeTable, useAdVxeTable, TableAction } from '@/components/AdVxeTable';
  import { readAuditOperations } from '@/api/audit/auditoperation';

  import { FilterOperateEnum } from '@/enums/filterOperateEnum';
  import { FilterRule, FilterGroup } from '@/utils/common/types';

  import { FormModeEnum } from '@/enums/formEnum';

  import { useModal } from '@/components/AdVxeModal';
  import AuditEntityModal from '@/views/system/audit/auditentity/AuditEntityModal.vue';

  // import { useDrawer } from '@/components/Drawer';
  // import AuditEntityDrawer from '@/views/system/audit/auditentity/AuditEntityDrawer.vue';

  import { columns } from './auditoperation.data';

  defineOptions({ name: 'AuditOperationPage' });

  const { t } = useI18n();

  const [registerTable, { openDetailForm }] = useAdVxeTable({
    id: 'system-audit-operation-page-table',
    // 表标题呈现器
    title: t('routes.system.audit.auditOperation.auditOperationList'),
    // 表格数据接口请求对象
    api: readAuditOperations,
    // 详情表单
    detailForm: {
      // 表单标识
      formIndex: 'audit_operation_detail',
      // 主键字段
      keyField: 'Id',
      // 详情表单抬头
      title: t('routes.system.audit.auditOperation.title'),
    },
    // 表单列信息 AdColumn[]
    columns,
    // 使用搜索表单
    searchForm: true,
    // 是否带有边框 boolean | default（默认）, full（完整边框）, outer（外边框）, inner（内边框）, none（无边框）
    border: false,
    // 是否显示序号列
    showSeqColumn: true,
    // 表格右侧操作列配置 AdColumn
    actionColumn: {
      width: 80,
      title: t('common.actionText'),
      fixed: 'right',
      slots: { default: 'actionCol' },
    },
  });

  const [registerAuditEntityModal, { openModal: openAuditEntityModal }] = useModal();

  // const [registerAuditEntityDrawer, { openDrawer: openAuditEntityDrawer }] = useDrawer();

  /** 查看操作审计详情 */
  async function handleReadDetail(record: any) {
    openDetailForm(FormModeEnum.ReadOnly, record);
  }

  /** 处理读取数据审计 */
  async function handleReadAuditEntries(record: any) {
    const filterGroup: FilterGroup = new FilterGroup();
    const filterRule: FilterRule = new FilterRule(
      'OperationId',
      record.Id,
      FilterOperateEnum.Equal,
    );
    filterGroup.Rules = [filterRule];

    openAuditEntityModal(true, {
      filterGroup: filterGroup,
    });

    // openAuditEntityDrawer(true, {
    //   filterGroup: filterGroup,
    // });
  }
</script>
